<?php

/**
 * User model
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: User.php 38 2009-02-18 17:12:39Z monzee $
 */
class User_User
{
    /**
     * @var Zend_Db_Table_Abstract
     */
    protected $_table;
    /**
     * @var Zend_Db_Table_Row_Abstract
     */
    protected $_row;
    protected $_tablePrefix = '';
    protected $_tableName = 'users';
    protected $_values = array();
    protected $_conn;

    /**
     *
     * @return Zend_Db_Table_Abstract
     */
    public function getTable()
    {
        if (null === $this->_table) {
            $table = $this->_tablePrefix . $this->_tableName;

            $tables = $this->getConnection()->listTables();
            if (!in_array($table, $tables)) {
                $this->createTable();
            }

            $this->_table = new User_Table(array(
                Zend_Db_Table_Abstract::NAME => $table));
        }

        return $this->_table;
    }

    public function setTable(Zend_Db_Table_Abstract $table)
    {
        $this->_table = $table;
        return $this;
    }

    public function setTablePrefix($prefix)
    {
        $this->_tablePrefix = $prefix;
        return $this;
    }

    public function setTableName($name)
    {
        $this->_tableName = $name;
        return $this;
    }

    public function create(array $values)
    {
        $this->_values = $this->_normalizeKeys($values);
        $this->_values['created_at'] = Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss');
        $this->_row = $this->getTable()->createRow();
        return $this;
    }

    public function update(array $values = null)
    {
        if (!empty($values)) {
            $this->_normalizeKeys($values);
        }
        $this->_values['updated_at'] = Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss');
        return $this;
    }

    public function save()
    {
        // TODO: check for uniqueness of username and email before saving
        $this->_row
            ->setFromArray($this->_values)
            ->save();

        return $this;
    }

    /**
     *
     * @return Zend_Db_Adapter_Abstract
     */
    public function getConnection()
    {
        if (null === $this->_conn) {
            $this->_conn = Zend_Db_Table_Abstract::getDefaultAdapter();
        }
        return $this->_conn;
    }

    public function setConnection($conn)
    {
        $this->_conn = $conn;
        return $this;
    }

    public function createTable($dropFirst = false)
    {
        $table = $this->_tablePrefix . $this->_tableName;
        $db = $this->getConnection();

        if ($dropFirst) {
            $db->query('DROP TABLE ' . $db->quoteIdentifier($table));
        }

        $sql = 'CREATE TABLE ' . $db->quoteIdentifier($table) . ' ( '
             . $db->quoteIdentifier('id') . ' INTEGER PRIMARY KEY AUTOINCREMENT, '
             . $db->quoteIdentifier('username') . ' VARCHAR(32), '
             . $db->quoteIdentifier('password') . ' VARCHAR(64), '
             . $db->quoteIdentifier('group') . ' VARCHAR(64), '
             . $db->quoteIdentifier('email') . ' VARCHAR(256), '
             . $db->quoteIdentifier('created_at') . ' DATETIME, '
             . $db->quoteIdentifier('updated_at') . ' DATETIME)';

        $db->query($sql);
        return $this;
    }

    protected function _normalizeKeys(array $array)
    {
        $ret = array();
        foreach ($array as $key => $value) {
            $key = strtolower(str_replace(' ', '_', $key));
            $ret[$key] = $value;
        }

        return $ret;
    }

}
